﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace ExcelSax.UT {
    [TestFixture]
    class DataReaderUT {        
        private ExcelSax.UT.RessourceLoader _l;
        private static String _baseDomaine = "ExcelSax.UT.Ressources";

        // ====================================================================
        [SetUp]
        public void Init() {
            _l = new ExcelSax.UT.RessourceLoader(ExcelSax.UT.RessourceLoader.ExcelSaxUTAssemblyName);            
        }
        // ====================================================================
        [Test]
        public void Can_get_the_good_number_of_columns_from_first_line() {
            String fn = _l.GetRandomFileName(".xlsx");
            _l.GetRessourceToFile(_baseDomaine + ".ReadGoodNumberOfColumns.xlsx", fn);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(fn)) {
                using (ExcelSaxDataReader esdr = esc.GetReaderForSheet("Feuil1")) {
                    esdr.Read();
                    Assert.That(esdr.FieldCount, Is.EqualTo(4));
                }
            }

            System.IO.File.Delete(fn);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_read_the_good_number_of_rows() {
            String fn = _l.GetRandomFileName(".xlsx");
            _l.GetRessourceToFile(_baseDomaine + ".ReadGoodNumberOfRows.xlsx", fn);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(fn)) {
                using (ExcelSaxDataReader esdr = esc.GetReaderForSheet("Feuil1")) {
                    while (esdr.Read()) ;
                    Assert.That(esdr.NumberOfCurrentLine, Is.EqualTo(3));
                }
            }

            System.IO.File.Delete(fn);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_read_the_good_number_of_EXCEL_rows() {
            String fn = _l.GetRandomFileName(".xlsx");
            _l.GetRessourceToFile(_baseDomaine + ".ReadGoodNumberOfRows.xlsx", fn);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(fn)) {
                using (ExcelSaxDataReader esdr = esc.GetReaderForSheet("Feuil1")) {
                    while (esdr.Read()) ;
                    Assert.That(esdr.NumberOfCurrentExcelLine, Is.EqualTo(10));
                }
            }

            System.IO.File.Delete(fn);
        }
        // --------------------------------------------------------------------
        [TestCase("A1", Result = 0)]
        [TestCase("ABC1", Result = 0)]
        [TestCase("AB10", Result = 9)]
        [TestCase("YZ153", Result = 152)]
        public Int32 Can_extract_row_number_from_well_formed_excel_cell_id(String cellId) {
            return ExcelSaxDataReader.GetRowNumberFromCellId(cellId);
        }
        // --------------------------------------------------------------------
        [TestCase("A1", Result = 0)]
        [TestCase("A10", Result = 0)]
        [TestCase("B1", Result = 1)]
        [TestCase("AA1", Result = 26)]
        public Int32 Can_extract_column_number_from_well_formed_excel_cell_id(String cellId) {
            return ExcelSaxDataReader.GetColIdFromCellId(cellId);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_read_the_good_strings_in_a_row() {
            String fn = _l.GetRandomFileName(".xlsx");
            _l.GetRessourceToFile(_baseDomaine + ".tests.xlsx", fn);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(fn)) {
                using (ExcelSaxDataReader esdr = esc.GetReaderForSheet("F1")) {
                    while (esdr.Read()) if (esdr.NumberOfCurrentLine == 4) break;
                    Assert.That(esdr[0], Is.EqualTo("c1"));
                    Assert.That(esdr[1], Is.EqualTo("c2"));
                    Assert.That(esdr[3], Is.EqualTo("c4"));
                    Assert.That(esdr[8], Is.EqualTo("c9"));
                }
            }

            System.IO.File.Delete(fn);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_read_the_good_DateTime_in_a_row() {
            String fn = _l.GetRandomFileName(".xlsx");
            _l.GetRessourceToFile(_baseDomaine + ".tests.xlsx", fn);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(fn)) {
                using (ExcelSaxDataReader esdr = esc.GetReaderForSheet("F1")) {
                    while (esdr.Read()) if (esdr.NumberOfCurrentLine == 3) break;
                    Assert.That(esdr[0], Is.EqualTo(new DateTime(1972,8,18)));                    
                }
            }

            System.IO.File.Delete(fn);
        }
    }
}
